﻿//#define USE_ASSETBUNDLE

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Asgard;
using UnityEngine.UI;


enum  ICON_TYPE
{
    AVATAR,
    SKILL,
    ITEM,
    RANK,
	CHAT,
    NUMBER,
    Team
}

public class SpriteResourceManager  {

    //图片精灵根路径
    private const string SPRITE_RESOURCE_ROOT = "Sprite/";
    //头像Icon资源路径
    private const string AVATAR_RESOURCE_PATH = "Avatar/";
    //任务Icon资源路径
    private const string ITEM_RESOURCE_PATH = "Item/";
    //技能Icon资源路径
    private const string SKILL_RESOURCE_PATH = "Skill/";
    //技能Icon资源路径
    private const string RANK_RESOURCE_PATH = "Rank/";
	//聊天Icon资源路径
	private const string CHAT_RESOURCE_PATH = "Chat/";
    //通用数字Icon资源路径
    private const string NUMBER_RESOURCE_PATH = "Number/";
    //组队icon资源路径
    private const string TEAM_RESOURCE_PATH = "Team/";

    static private SpriteResourceManager _instance;

    private Dictionary<string, Sprite> _spritesCacheDic = new Dictionary<string, Sprite>();
    private AssetBundle _avatarAssetBundle = null;
    private AssetBundle _skillAssetBundle = null;
    private AssetBundle _taskAssetBundle = null;

    /// <summary>
    /// 图片资源管理实例
    /// </summary>
    public static SpriteResourceManager Instance
    {
        get
        {
            if (_instance == null)
                _instance = new SpriteResourceManager();
            return _instance;
        }
    }

    /// <summary>
    /// 通过资源类型 和资源名称获取图片精灵
    /// </summary>
    /// <param name="resType">资源类型</param>
    /// <param name="resName">资源名称</param>
    /// <returns></returns>
    public Sprite GetSprite(uint iconType, string resName)
    {
        Sprite resultSprite = new Sprite();
        if(_spritesCacheDic.ContainsKey(resName))
        {
            _spritesCacheDic.TryGetValue(resName, out resultSprite);
        }
        else
        {
            resultSprite = LoadSprite(iconType, resName);
        }

        return resultSprite;  
    }

    /// <summary>
    ///  加载图片精灵
    /// </summary>
    /// <param name="resType">资源类型</param>
    /// <param name="resName">资源名称</param>
    /// <returns></returns>
    private Sprite LoadSprite(uint iconType, string resName)
    {
#if USE_ASSETBUNDLE
        AssetBundle assetBundle = GetAssetBundleByType(resType);
        return assetBundle.Load(resName) as Sprite;
        
#else
        string resPath = GetSpritePathByType(iconType);

            if (resPath != null)
            {
                GameObject resultGo = ResourceManager.LoadResource<GameObject>(resPath + resName);
                return resultGo.GetComponent<SpriteRenderer>().sprite;
            }

            return null;
#endif
    }


    /// <summary>
    /// 获取精灵资源加载路径
    /// </summary>
    /// <param name="resType">资源类型</param>
    /// <returns></returns>
    private string GetSpritePathByType(uint iconType)
    {
        string resPath = null;
        switch (iconType)
        {
            case (uint)ICON_TYPE.AVATAR:
                resPath = SPRITE_RESOURCE_ROOT + AVATAR_RESOURCE_PATH;
                break;
            case (uint)ICON_TYPE.ITEM:
                resPath = SPRITE_RESOURCE_ROOT + ITEM_RESOURCE_PATH;
                break;
            case (uint)ICON_TYPE.SKILL:
                resPath = SPRITE_RESOURCE_ROOT + SKILL_RESOURCE_PATH;
                break;
            case (uint)ICON_TYPE.RANK:
                resPath = SPRITE_RESOURCE_ROOT + RANK_RESOURCE_PATH;
                break;
			case (uint)ICON_TYPE.CHAT:
				resPath = SPRITE_RESOURCE_ROOT + CHAT_RESOURCE_PATH;
				break;
            case (uint)ICON_TYPE.NUMBER:
                resPath = SPRITE_RESOURCE_ROOT + NUMBER_RESOURCE_PATH;
                break;
            case (uint)ICON_TYPE.Team:
                resPath = SPRITE_RESOURCE_ROOT + TEAM_RESOURCE_PATH;
                break;
        }

        return resPath;
    }

    /// <summary>
    /// 通过资源类型获取AssetBundle
    /// </summary>
    /// <param name="resType">资源类型</param>
    /// <returns></returns>
    private AssetBundle GetAssetBundleByType(uint iconType)
    {
        AssetBundle resultAB = null;
        switch (iconType)
       {
           case (uint)ICON_TYPE.AVATAR:
               resultAB = _avatarAssetBundle;
               if(resultAB == null)
                   resultAB = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/AvatarIcon.assetbundle");
               break;
           case (uint)ICON_TYPE.SKILL:
               resultAB = _skillAssetBundle;
               if (resultAB == null)
                   resultAB = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/SkillIcon.assetbundle");
               break;
           case (uint)ICON_TYPE.ITEM:
               resultAB = _taskAssetBundle;
               if (resultAB == null)
                   resultAB = AssetBundle.CreateFromFile(Application.streamingAssetsPath + "/TaskIcon.assetbundle");
               break;
       }

       return resultAB;
    }
}
